/*
 * DA2DiscoveryListener.java
 *
 * Created on 11 marzo 2007, 21.56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bloomingstars.bluepad.comm;

import com.bloomingstars.bluepad.util.BTUtils;
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

/**
 *
 * @author toni
 */
public class DA2DiscoveryListener implements DiscoveryListener
{    
    public DA2DiscoveryListener(BTDA2Comm comm)
    {
        this.comm = comm;
    }
      
    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass)
    {        
        try
        {
            String deviceName = remoteDevice.getFriendlyName(false);
            System.out.println("deviceDiscovered: " + deviceName);
            
            // Trigger Serial Port Service Discovery
            if (deviceName.equalsIgnoreCase(comm.getAddress().getDeviceName()))
            {
                remoteDevicesDiscovered.addElement(remoteDevice);
            }            
        } 
        catch (IOException e)
        {
            System.err.println("Problems while getting the friendly name of a device");
            e.printStackTrace();
        }
    }
    
    public void inquiryCompleted(int complete)
    {
        System.out.println("inquiryCompleted!");
        // Notify listening threads
        
        switch (complete)
        {
            case INQUIRY_COMPLETED:
                comm.setSetupStatus(SetupStatus.SETUP_OK);
                break;
                
            case INQUIRY_ERROR:
            case INQUIRY_TERMINATED: 
                comm.setSetupStatus(SetupStatus.SETUP_DEVICE_SEARCH_FAILED);
                break;
        }
        
        synchronized (comm)
        {
            comm.notify();
        }
            
    }
    
    public void servicesDiscovered(int transId, ServiceRecord[] records)
    {     
        System.out.println("servicesDiscovered!");
        for(int i = 0; i < records.length; i++)
        {
            ServiceRecord record = records[i];
            BTUtils.printServiceRecord(record);
            // printService(record);                
            servicesRecordsDiscovered.addElement(record);
        }
    }
    
    public void printService(ServiceRecord rec)
    {
        int[] attrIds = rec.getAttributeIDs();        
        System.out.println("URL: " + rec.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false));
        
        for (int i=0; i<attrIds.length; i++) 
        {
            DataElement el = rec.getAttributeValue(attrIds[i]);
            System.out.println("Attribute " + attrIds[i] + " : " + el.getDataType());
        }
        System.out.println();
        DataElement el = rec.getAttributeValue(256);
        if (el != null && el.getDataType() == 32)
        {
            System.out.println("Service name: " + (String) el.getValue());
        }
    }
        
    
    public void serviceSearchCompleted(int transId, int complete)
    {
        System.out.println("serviceSearchCompleted!");
        switch (complete)
        {            
            case SERVICE_SEARCH_NO_RECORDS:
                System.out.println("\n SERVICE_SEARCH_NO_RECORDS\n");
                comm.setSetupStatus(SetupStatus.SETUP_SERVICE_NOT_FOUND);
                break;
                
            case SERVICE_SEARCH_COMPLETED:
                System.out.println("\nSERVICE_SEARCH_COMPLETED\n");
                comm.setSetupStatus(SetupStatus.SETUP_OK);
                break;
                
            case SERVICE_SEARCH_ERROR:            
                System.out.println("\nSERVICE_SEARCH_ERROR\n");
                comm.setSetupStatus(SetupStatus.SETUP_SERVICE_SEARCH_FAILED);
                break;
                
            case SERVICE_SEARCH_TERMINATED:
                System.out.println("\n SERVICE_SEARCH_TERMINATED\n");
                comm.setSetupStatus(SetupStatus.SETUP_SERVICE_SEARCH_FAILED);
                break;
                
            case SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
                System.out.println("\n SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
                comm.setSetupStatus(SetupStatus.SETUP_SERVICE_SEARCH_FAILED);
                break;
        }
        
         // Notify listening threads
        synchronized (comm)
        {
            comm.notify();
        }    
    }
    
    public Vector getRemoteDevicesDiscovered()
    {
        return remoteDevicesDiscovered;
    }
    
    public Vector getServicesDiscovered()
    {
        return servicesRecordsDiscovered;
    }    
    
    private Vector remoteDevicesDiscovered = new Vector();
    private Vector servicesRecordsDiscovered = new Vector();
    private Vector transactionIds = new Vector();
    private BTDA2Comm comm;
    private int serviceSearchResult = -1;
    private int deviceSearchResult = -1;
}

